package com.yijushequ.action.user;

import com.yijushequ.action.BasicAction;
import com.yijushequ.dao.BaseUserDao;
import com.yijushequ.dao.CommunityDao;
import com.yijushequ.domain.BaseUser;
import com.yijushequ.domain.Community;
import com.yijushequ.domain.CommunityUserRelation;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@SuppressWarnings("UnusedDeclaration")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace(BasicAction.BASE_NAME_SPACE)
public class UserAction extends BasicAction {

    private BaseUser baseUser;
    private BaseUserDao baseUserDao;
    private CommunityDao communityDao;


    @Action(value = "createSession",
            results = {
                    @Result(name = SUCCESS, type = "tiles", location = ".index")
            }
    )
    public String createSession() throws Exception {
        if (this.getSessionOpenId() == null) {
            BaseUser tmpUser = baseUserDao.findByOpenId(this.getBaseUser().getOpenId());
            if (tmpUser == null) {
                baseUserDao.persist(this.getBaseUser());
                saveCommunityUserRelation();

            } else {
                tmpUser.setNickName(this.getBaseUser().getNickName());
                tmpUser.setFigureUrl(this.getBaseUser().getFigureUrl());
//                tmpUser.setRegTime(this.getBaseUser().getRegTime());
                this.baseUserDao.persist(tmpUser);
                this.setBaseUser(tmpUser);
                Community community = communityDao.findById(super.getSessionCommunity());
                if (!this.getBaseUser().getCommunityUserRelations().contains(community)) {
                    saveCommunityUserRelation();
                }
            }
            this.setUserToSession(baseUser);
        }
        return NONE;
    }

    private void saveCommunityUserRelation() {
        CommunityUserRelation communityUserRelation = new CommunityUserRelation();
        communityUserRelation.setBaseUser(this.getBaseUser());
        Community community = communityDao.findById(super.getSessionCommunity());
        communityUserRelation.setCommunity(community);
        super.getModelDao().persist(communityUserRelation);
    }

    @Action(value = "removeUserSession",
            results = {
                    @Result(name = SUCCESS, type = "tiles", location = ".index")
            }
    )
    public String removeUserSession() throws Exception {
        super.removeSession();
        return NONE;
    }

    public BaseUser getBaseUser() {
        return baseUser;
    }

    public void setBaseUser(BaseUser baseUser) {
        this.baseUser = baseUser;
    }

    public BaseUserDao getBaseUserDao() {
        return baseUserDao;
    }

    public void setBaseUserDao(BaseUserDao baseUserDao) {
        this.baseUserDao = baseUserDao;
    }

    public CommunityDao getCommunityDao() {
        return communityDao;
    }

    public void setCommunityDao(CommunityDao communityDao) {
        this.communityDao = communityDao;
    }
}
